import threading
import time
from concurrent.futures import ThreadPoolExecutor

import json
import threading
from datetime import datetime

import requests

executor = ThreadPoolExecutor(max_workers=10000)
lock = threading.Lock()

x: int = 0


def call_api():
    url = "http://192.168.80.51:19302/query"
    # url = "http://192.168.80.51:8088/economic/policy/ask"
    json_data = json.dumps({'question': '你能告诉我关于机器学习的事情吗？', 'type': '1'})
    response = requests.post(url, data=json_data)
    print(response.text)  # 输出响应内容


# 定义一个任务函数
def task(n):
    print(f"Task {n} started")
    time.sleep(n)
    global x
    x=x+1
    #print(x)
    return f"Task {n} completed"
    # 创建 ThreadPoolExecutor
    # with ThreadPoolExecutor(max_workers=10) as executor:  # 控制线程池大小为3

"""
threads = []

for i in range(5):
    future = executor.submit(task, i)
    threads.append(future)

for i in threads:
    print(i.result())
"""
t1 = datetime.now()
future1 = executor.submit(task, 1)
future2 = executor.submit(task, 5)
future3 = executor.submit(task, 10)

# 获取任务执行结果
print(future1.result())
second = (datetime.now()-t1).total_seconds()
print(f"耗时{second}")
print(future2.result())
second = (datetime.now()-t1).total_seconds()
print(f"耗时{second}")
print(future3.result())
second = (datetime.now()-t1).total_seconds()
print(f"耗时{second}")



